﻿using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Resources;
using System.Windows;
using AzureDiagMon.Common.Mvvm.Interfaces;

namespace AzureDiagMon.Common.Mvvm
{
    /// <summary>
    /// Generic view model for child controls (for dialog)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ChildViewModel : ViewModel, IChildViewModel
    {
        private string title;// = Resources.AppTitle;

        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged("Title");
            }
        }

        protected ChildViewModel()
        {
            SetTitleFromResource();
        }

        private void SetTitleFromResource()
        {
            //TODO: Read resource on Windows Phone
#if !WINDOWS_PHONE
            var resourceName = String.Concat(GetType().Name.Replace("ViewModel", String.Empty), "Title");
            var a = GetType().Assembly;
            var resourcesToCheck = a.GetManifestResourceNames();
            foreach (string s in resourcesToCheck)
            {
                using (var stream = a.GetManifestResourceStream(s))
                {
                    if (stream != null)
                    {
                        using (var reader = new ResourceReader(stream))
                        {
                            foreach (DictionaryEntry entry in reader)
                            {
                                if (entry.Key.ToString() == resourceName)
                                {
                                    title = entry.Value as String;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
#endif
        }

        public virtual void HandleWindowClosing(CancelEventArgs args)
        {
        }

        public virtual bool HandleOkCommand()
        {
            return true;
        }

        public virtual bool IsOkEnabled()
        {
            return true;
        }

        public virtual bool HandleCancelCommand()
        {
            return true;
        }

        public virtual bool IsCancelEnabled()
        {
            return true;
        }
    }
}
